const mongoose = require('mongoose');
var express = require('express')
var jobs = express()
const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {post_service } = require('../../service/impl/post_service/index');



jobs.post('/post/jobs/add', async (req,res) => {
  console.log(req.body.news)
  let result
  try {
    let news  = req.body.news
    result =  await post_service.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  jobs.post('/post/jobs/delete', async (req,res) => {
    let result
    console.log(req.body,66);
    try {
      let  id  = req.body.conditions.id
      result =  await post_service.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  jobs.post('/post/jobs/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await post_service.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  jobs.post('/post/jobs/query', async (req,res) => {
    let result
    let fills ={
      "key1":{
        "path":"job_companyID"
      },
      "key2":{
        "path":"job_publisherID"
      },
      "key3":{
        "path":"profession_id"
      },
      "key4":{
        "path":"profession_rank.rank"
      },
    }
    try {
      result =  await post_service.getIns().find(null,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })



  // 使招聘信息通过/不通过审核
  jobs.post('/post/jobs/updateByIdAccess', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await post_service.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


  jobs.post('/post/job/query_post_by_profession_id', async (req,res) => {
    let result
    let fills ={
      "key1":{
        "path":"job_companyID"
      },
      "key2":{
        "path":"job_publisherID"
      },
      "key3":{
        "path":"profession_id"
      },
      "key4":{
        "path":"profession_rank.rank"
      },
    }


    try {
      let conditions = {
        profession_id:req.body.conditions.profession_id
      }

      console.log( conditions,222)
      result =  await post_service.getIns().find(conditions,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })

  
  // 根据公司ID查询该公司的招聘所有信息
  jobs.post('/post/job/query_post_by_team_id', async (req,res) => {
    let result
    let fills ={
      "key1":{
        "path":"job_companyID"
      },
      "key2":{
        "path":"job_publisherID"
      },
      "key3":{
        "path":"profession_id"
      },
      "key4":{
        "path":"profession_rank.rank"
      },
    }

    try {
      let conditions = {
        job_companyID:req.body.conditions.job_companyID
      }
      result =  await post_service.getIns().find(conditions,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


  
  

  module.exports= jobs